import {
  productInfo,
  userConfirm,
  userConfirmDelivery,
  defaultBankcard,
  cancelSforder
} from "../../../api.js"
var commodityStatus = [
  '卖家待邮寄，请尽快寄出哦',
  '平台待收货，请耐心等待哦',
  '平台审核中，请耐心等待哦',
  '待卖家确认，请尽快确认哦',
  '商品退回中，请耐心等待哦',
  '商品退回中，请耐心等待哦',
  '已上线，请耐心等待哦',
  '待结算，请耐心等待1-7个工作日',
  '已结算，请注意查收',
  '寄卖失败，平台审核未通过',
  '寄卖失败，卖家取消寄卖',
  '寄卖失败，卖家确认收货'
]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    head_text: '',
    product_id: '',
    product_cen: [],
    bankcard: [],//默认银行卡
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id
    this.setData({
      product_id: id
    })
    this.init(options.id)
  },
  onShow(){
    var that = this
    defaultBankcard(
      getApp().globalData.Store.token
    ).then(res => {
      if (res.data.code == 666) {
        that.setData({
          bankcard: res.data.data
        })
      }
    })
    getApp().startpage(new Date().getTime())
  },
  // 页面加载
  init(id) {
    var id = Number(id)
    var that = this
    productInfo(
      getApp().globalData.Store.token,
      id
    ).then(res => {
      if (res.data.code == 666) {

        var text = ''
        var text_state = res.data.data.product_resell_state - 1
        var text_state_li = res.data.data
        console.log(text_state)
        console.log(text_state_li)
        if (text_state == 9 && text_state_li.is_confirm == 2) {
          text = commodityStatus[9]
        } else if (text_state == 9 && text_state_li.is_user_confirm == 2) {
          text = commodityStatus[10]
        } else if (text_state == 9 && text_state_li.user_confirm_delivery == 1) {
          text = commodityStatus[11]
        } else {
          text = commodityStatus[text_state]
        }
        that.setData({
          product_cen: res.data.data,
          head_text: text,
        })
      }
    })
    
  },
  godelivery() {
    wx.navigateTo({
      url: '/pages/consignment/delivery/delivery?id=' + this.data.product_cen.id,
    })
  },
  userReject(e) {
    var num = e.currentTarget.dataset.index
    var that = this
    wx.showModal({
      title: '提示',
      content: num == 1 ? '是否同意' : '是否拒绝',
      success(res) {
        if (res.confirm) {
          userConfirm(
            getApp().globalData.Store.token,
            that.data.product_id,
            num
          ).then(res => {
            console.log(res)
            if (res.data.code == 666) {
              that.init(that.data.product_id)
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //确认收货
  confirm() {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success(res) {
        if (res.confirm) {
          userConfirmDelivery(
            getApp().globalData.Store.token,
            that.data.product_id,
          ).then(res => {
            if (res.data.code == 666) {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
              that.init(that.data.product_id)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //银行卡列表
  gobankcardLis() {
    console.log('2')
    wx.navigateTo({
      url: '/pages/myList/bankCard/bankCard',
    })
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  },
})